# controls the buildstream

# designed to run from the model's top level directory

# ------------------------------------------------------------------------------
# parameters
# ------------------------------------------------------------------------------

# sector, region, household aggregation mapping file
aggregation.file = "default_aggregation.gms"

# use the name of the aggregation file as the name of the aggregated benchmark
benchmark.file = paste0(strsplit(aggregation.file, "\\.")[[1]][1],".gdx")

# option to calibrate benchmark investment to a balanced growth path
balanced_growth = 1



# ------------------------------------------------------------------------------
# build the parameters
# ------------------------------------------------------------------------------

source(file.path("build","build_parameters.R"))



# ------------------------------------------------------------------------------
# get data needed to disaggregate oil and gas extraction
# ------------------------------------------------------------------------------

# download and process the external oil and gas data
source(file.path("build","get_oil_and_gas_data.R"))



# ------------------------------------------------------------------------------
# get the benchmark population data
# ------------------------------------------------------------------------------

# uses data from the cps to estimate the number of households each
# representative agent in the model represents. warning, it takes some time for
# the script to download and process the cps dataset and the dependencies take
# a while to download and compile as well.

# download and process the external oil and gas data
source(file.path("build","get_population_data.R"))



# ------------------------------------------------------------------------------
# extract implan data for use in gams
# ------------------------------------------------------------------------------

# this step requires that the implan state level social accounting matrix data
# is located in build\data\implan. this is exported from the implan desktop
# software by creating a model containing only a single state built through
# multipliers. then clicking on social accounts, IxC social accounting matrix,
# export, industry detail sam files, gams single file. the gams single files
# should be labeled with the lower case two letter state abbreviation, such that
# state xx has its data stored in build\data\implan\xx.gms. as far as we can
# tell the export step needs to be done individually for each state, but some
# time can be saved by batch creating the state level implan models using the
# batch model definition in build\data\implan\batch state models.xls. if implan
# data from a different year are used and the sectors or households have changed
# additional changes in the buildstream code are required.

# extract the implan data for each state
for (state in c(tolower(state.abb),"dc")) {
  print(paste0("extracting implan data for ",state,"..."))
  system(paste0("gams build\\\\read_implan_state.gms --state=",state))
}

# merge the state level files into a single dataset
system("gdxmerge build\\\\data\\\\implan\\\\*.gdx")

# move the merged dataset
file.copy("merged.gdx",file.path("build","data","merged.gdx"),overwrite=TRUE)
file.remove("merged.gdx")

# remove the state level gdx files
files = dir(file.path("build","data","implan"))
for (file in files[grep(".gdx",files)])
  file.remove(file.path("build","data","implan",file))



# ------------------------------------------------------------------------------
# build benchmark from implan data
# ------------------------------------------------------------------------------

# build the balanced benchmark from the implan data
# build data, convert to the model's namspace, and add taxes, disaggregate oil
# and gas extraction, and add population
system("gams build\\\\build_benchmark.gms")



# ------------------------------------------------------------------------------
# aggregate benchmark
# ------------------------------------------------------------------------------

# command to aggregate the benchmark
command = paste0("gams build\\\\aggregate_benchmark.gms ",
                 "--map_file=build\\\\aggregation_map\\\\",aggregation.file,
                 " --output_file=data\\\\",benchmark.file)

# run the command
system(command)



# ------------------------------------------------------------------------------
# balance benchmark and filter small values
# ------------------------------------------------------------------------------

if (length(sage.years)==1) {
  interval = 1
} else {
  interval = sage.years[2]-sage.years[1]
}

# the model updates the rates based on the time step seperately but the
# balancing routine just takes the real growth rate. so send it the single
# growth rate that will match the model based on the way it treats the time step
growth_rate = ((1+omega)^interval-1+(1+gamma)^interval-1+1)^(1/interval)-1

# command to balance the benchmark
command = paste0("gams build\\\\balance_benchmark.gms ",
                 "--benchmark_file=data\\\\",benchmark.file,
                 " --output_file=data\\\\",benchmark.file,
                 " --balanced_growth=",balanced_growth,
                 " --delta=",delta,
                 " --growth_rate=",growth_rate,
                 " --rbar=",rbar,
                 " --interval=",interval)

# run the command
system(command)
